(function($) {
	"use strict";
	$.fn.extend({
		lazyOptions:{},
		isViewport: function($this,obj) {
			var elTop = $this.offset().top-obj.offsetTop;
			return elTop>=0&&elTop<=(obj.offsetHeight+obj.offsetBottom);
		},
		imgLoad: function($this,obj) {
			var img = new Image(),
				original = $this.attr('data-src');
			img.onload = function() {
				$this.attr('src', original).removeClass('lazy');
				obj.anim && $this.css({opacity:0.1}).animate({opacity: 1}, obj.time,function(){
					if($this.attr("src")==obj.placeholder){			//iosBUG-再次验证并适时载入图像
						$this.attr("src",original).removeClass('lazy');
					}
					img=null;
				});
			};
			original&&(img.src = original);
		},
		lazyload: function(options){
			var that = this;
			if(options instanceof Object){
				if(Object.keys(that.lazyOptions).length==0){
					var opt = {
						anim:true,
						time:500,
						offsetTop:0,
						offsetBottom:0,
						offsetHeight:0,
						placeholder:""
					}
					$.extend(opt,options);
					that.lazyOptions=opt;
				}
			}
			if(Object.keys(that.lazyOptions).length>0){
				that.each(function(i,dom) {
					var $this=$(this);
					!$this.attr("src")&&$this.attr("src",opt.placeholder);
					that.isViewport($this,that.lazyOptions)&&that.imgLoad($this,that.lazyOptions);
				})
			}
			return that;
		}
	})
})(jQuery)